package com.demo.common.permission;

import com.demo.common.cache.CacheService;
import com.demo.common.utils.CacheKeyConstants;
import com.demo.service.SysRolePermissionService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Component
public class PermissionService {

    @Resource
    private CacheService cacheService;

    @Resource
    private SysRolePermissionService sysRolePermissionService;

    /**
     * 获取用户所有权限
     * @param userId
     * @return
     */
    public Set<String> getUserPermission(Integer userId){
        UserPermissionCollect userPermissionCollect=this.getUserPermissionCollect(userId);
        return userPermissionCollect.getPermissionSet();
    }

    /**
     * 判断用户是否有某个权限
     * @param userId
     * @param permission
     * @return
     */
    public boolean hasPermission(Integer userId,String permission){
        UserPermissionCollect userPermissionCollect=this.getUserPermissionCollect(userId);
        return userPermissionCollect.getPermissionSet().contains(permission);
    }

    /**
     * 判断用户是否有指定的所有权限
     * @param userId
     * @param permissions
     * @return
     */
    public boolean hasPermissionAll(Integer userId,String[] permissions){
        UserPermissionCollect userPermissionCollect=this.getUserPermissionCollect(userId);
        Set<String> permissionSet = userPermissionCollect.getPermissionSet();
        for (String permission : permissions) {
            //其中一个权限没有就返回false
            if(!permissionSet.contains(permission)){
                return false;
            }
        }
        return true;
    }

    /**
     * 判断用户是否有指定的权限中的任意一个
     * @param userId
     * @param permissions
     * @return
     */
    public boolean hasPermissionRandom(Integer userId,String[] permissions){
        UserPermissionCollect userPermissionCollect=this.getUserPermissionCollect(userId);
        Set<String> permissionSet = userPermissionCollect.getPermissionSet();
        for (String permission : permissions) {
            //有其中一个权限就返回true
            if(permissionSet.contains(permission)){
                return true;
            }
        }
        return false;
    }

    /**
     * 清除缓存
     * @param userIds
     */
    public void clearCache(List<Integer> userIds){
        for (Integer userId : userIds) {
            cacheService.removeCache(CacheKeyConstants.USER_PERMISSION+userId);
        }
    }


    /**
     * 获取用户权限集合，先读缓存，读不到再读db
     * @param userId
     * @return
     */
    private UserPermissionCollect getUserPermissionCollect(Integer userId){
        String userPermissionKey=CacheKeyConstants.USER_PERMISSION + userId;
        UserPermissionCollect userPermissionCollect = cacheService.getCache(userPermissionKey);
        if(userPermissionCollect==null){
            List<String> userPermission = sysRolePermissionService.getUserPermission(userId);
            userPermissionCollect=new UserPermissionCollect();
            userPermissionCollect.setPermissionSet(new HashSet<>(userPermission));
            //缓存7天
            cacheService.setCacheIfAbsent(userPermissionKey,userPermissionCollect,60*60*24*7);
        }
        return cacheService.getCache(userPermissionKey);
    }
}
